package com.lvxingli.generateannotation.action.intention;

import com.lvxingli.generateannotation.action.intention.base.AbstractUniversalGenerateIntentionAction;
import com.lvxingli.generateannotation.constants.ActionEnum;
import com.lvxingli.generateannotation.constants.IntentionFamilyEnum;
import com.lvxingli.generateannotation.factory.CommandCommandTypeProcessorFactory;
import com.lvxingli.generateannotation.pojo.SelectWrapper;
import com.lvxingli.generateannotation.pojo.WriteCommand;
import com.lvxingli.generateannotation.pojo.WriteContext;
import com.lvxingli.generateannotation.util.DecisionUtils;
import com.lvxingli.generateannotation.util.builder.WriteContextIntentionBuilder;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

/**
 * 通过当前元素名，在项目中匹配注释，并填充到JavaDoc中
 *
 * @author lvxingli
 */
@Slf4j
public class PopulateElementNameToJavaDocForElementIntentionAction extends AbstractUniversalGenerateIntentionAction {

    @Override
    protected boolean calcIsAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
        SelectWrapper selectWrapper = DecisionUtils.parseSelectWrapper(project, editor, element);
        if (!selectWrapper.isSelected()) {
            return false;
        }
        return selectWrapper.getField() != null || selectWrapper.getMethod() != null;
    }

    @Override
    protected void doInvoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
        WriteContext writeContext = WriteContextIntentionBuilder.create(project, editor, element);
        if (!writeContext.fetchSelected()) {
            log.warn("未选择任何元素 : {}", getClass().getName());
            return;
        }
        CommandCommandTypeProcessorFactory.decide(writeContext, WriteCommand.Command.POPULATE_ELEMENT_NAME_TO_JAVA_DOC).writeElement();
    }

    @Override
    protected IntentionFamilyEnum fetchIntentionFamilyEnum() {
        return IntentionFamilyEnum.SEARCH_ELEMENT_NAME_TO_JAVA_DOC;
    }

    @Override
    protected ActionEnum fetchActionEnum() {
        return ActionEnum.POPULATE_ELEMENT_NAME_TO_JAVA_DOC_FOR_ELEMENT;
    }
}
